package com.snsoft.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.snsoft.bean.User;
import com.snsoft.util.JdbcUtils;

/**
 * 
 * @author 慎伟康
 * 
 * @date 2017年10月21日 下午7:23:12
 * 
 * @Description TODO
 *     user模块dao
 */
public class UserDao {

	/**
	 * 根据账号获取用户
	 * 
	 * @param userCode
	 * @return User
	 * @throws SQLException
	 */
	public User getUser(String userCode) throws SQLException {
		QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "select * from users where userCode = ? and deleteFlag = '1'";
		User user = runner.query(sql, new BeanHandler<User>(User.class),
				new Object[] { userCode });
		return user;
	}

	/**
	 * 根据id获取用户
	 * 
	 * @param userID
	 * @return User
	 * @throws SQLException 
	 */
	public User getUser(int userID) throws SQLException {
		QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "select * from users where userID = ? and deleteFlag = '1'";
		User user = runner.query(sql, new BeanHandler<User>(User.class),
				new Object[] { userID });
		return user;
	}
	
	/**
	 * 用户修改密码
	 * @param userID
	 * @param password
	 * @throws SQLException
	 */
	public void updatePassword(int userID, String password) throws SQLException{
		QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "update users set userPassWord = ? where userID = ?";
		runner.update(sql, new Object[]{password, userID});
	}
	
	
}
